﻿using HSharp.Utils.AspNetCoreExtensions;
using Microsoft.AspNetCore.Mvc;
using QueueUtils.Models;
using System.Threading.Channels;

namespace APIDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class MsgChannelController : ControllerBase
    {
        private readonly Channel<MessageData> channel;

        private readonly ILogger logger; 
         
        public MsgChannelController(Channel<MessageData> channel, ILogger<MsgChannelController> logger)
        {
            this.channel = channel;
            this.logger = HSharpLogger.GetLogger();
        }

        [HttpGet]
        public async Task<IActionResult> HandelMsg()
        {
            var msgList = Enumerable.Range(1, 5).Select(index => new MessageData
            {
                Id = Random.Shared.Next(1, 99999),
                MsgTitle = Random.Shared.Next(-20, 55) + "这里是用户内容类信息",
                Msg = DateTime.Now.ToString("yyyyMMddHHmmssfff")


            });

            foreach (var item in msgList)
            {

                var _HSharpLogger = HSharpLogger.GetLogger(); 

                _HSharpLogger.LogInformation($"_HSharpLogger========666666======={item.MsgTitle}{item.Msg}");


                logger.LogInformation($"logger消息发布成功======77777777777777777========={item.MsgTitle}{item.Msg}");


                HSharpLogger.LogInformation($"HSharpLogger消息发布成功==============={item.MsgTitle}{item.Msg}");

                HSharpLogger.LogError($"HSharpLogger消息发布成功==============={item.MsgTitle}{item.Msg}");
                await channel.Writer.WriteAsync(item);
            } 
            return Ok("消息发布成功");
        }
    }
}
